package com.yang.controller.admin;

import com.yang.bean.MarketStorage;
import com.yang.bean.common.BasePageInfo;
import com.yang.bean.common.BaseRespVo;
import com.yang.bean.common.CommonData;
import com.yang.service.admin.StorageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.yang.service.admin.GoodsCategoryService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author: 杨庆
 * @since: 2022.9.7 08:14
 * @description:
 */
@RestController
@RequestMapping("admin/storage")
public class AdminStorageController {

    @Autowired
    GoodsCategoryService goodsCategoryService;

    /**
     * @description: 文件上传功能
     * @return: com.yang.bean.common.BaseRespVo
     * @author: 杨庆
     * @time: 2022.9.7 8:17
     */

    @RequestMapping("create")
    public BaseRespVo createStorage(MultipartFile file) {
        //TODO 获取文件的名称
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isEmpty(originalFilename)) {
            return BaseRespVo.error("上传文件失败");
        }
//        //TODO 配置本地的文件目录
//        File path = GenerateFilePath.getPath(originalFilename);
//        try {
//            file.transferTo(path);
//        } catch (IOException e) {
//            return BaseRespVo.error("上传文件失败");
//        }
//        //TODO 上传成功需要插入到表中
//        String contentType = file.getContentType();
//        long size = file.getSize();
//        MarketStorage marketStorage = new MarketStorage();
//        marketStorage.setAddTime(new Date());
//        marketStorage.setName(originalFilename);
//        marketStorage.setSize((int) size);
//        marketStorage.setType(contentType);
//        marketStorage.setUrl(Constant   .URL+"static/"+path.getName());
//        marketStorage.setKey(path.getName());
//        MarketStorage m2 = goodsCategoryService.createStorage(marketStorage);
        MarketStorage m2=null;
        try {
             m2  =goodsCategoryService.fileupload(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(m2);

    }




    

    @Autowired
    StorageService storageService;
    /**
     *  对象存储-list
     * @param info
     * @return com.yang.bean.common.BaseRespVo
     * @author BaoZi
     * @since 2022/09/07 14:54
     */
    @RequestMapping("list")
    public BaseRespVo addStorage(BasePageInfo info){
        CommonData<MarketStorage> data = storageService.queryList(info);
        BaseRespVo<CommonData> dateBaseRespVo = new BaseRespVo<>();
        return dateBaseRespVo.ok(data);
    }

    /**
     *  对象存储-update
     * @param market
     * @return com.yang.bean.common.BaseRespVo
     * @author BaoZi
     * @since 2022/09/07 14:53
     */
    @RequestMapping("update")
    public BaseRespVo updateStorage(@RequestBody  MarketStorage market){
        storageService.modifyStorage(market);
        return BaseRespVo.ok(market);
    }

    /**
     * 对象存储-delete
     * @param market
     * @return com.yang.bean.common.BaseRespVo
     * @author BaoZi
     * @since 2022/09/07 15:11
     */
    @RequestMapping("delete")
    public BaseRespVo deleteStorage(@RequestBody MarketStorage market){
        storageService.deleteStorage(market);
        return BaseRespVo.ok();
    }

}
